/**
 * 初始化用户详情对话框
 */
var merchantInfoDlg = {
    merchantInfoData: {},
    itemTemplate: $("#itemTemplate").html(),//用户信息
};


/**
 * 关闭此对话框
 */
merchantInfoDlg.close = function () {
    parent.layer.close(window.parent.WzUser.layerIndex);
}


/**
 * 提交修改
 */
merchantInfoDlg.update = function (i, userInfoId) {
    var merchantTel = $("#merchantTel" + i).val();
    var merchantAddress = $("#merchantAddress" + i).val();
    var merchantEmail = $("#merchantEmail" + i).val();
    var merchantWebsite = $("#merchantWebsite" + i).val();
    var merchantLongitude = $("#merchantLongitude" + i).val();
    var merchantLatitude = $("#merchantLatitude" + i).val();
    var merchantInfo = "{\"merchantInfo\":\"" + i + "\",\"merchantTel\":\"" + merchantTel + "\",\"merchantAddress\":\"" + merchantAddress + "\",\"merchantEmail\":\""
        + merchantEmail + "\",\"merchantWebsite\":\"" + merchantWebsite + "\",\"merchantLongitude\":\"" + merchantLongitude + "\",\"merchantLatitude\":\"" + merchantLatitude + "\"}";

    if(merchantTel==""){
        $("#merchantTel" + i).css("border","1px solid red");
        $("#telEmpty" + i).css("display","block");
        return false;
    }else{
        $("#merchantTel" + i).css("border","1px solid #E5E6E7");
        $("#telEmpty" + i).css("display","none");
    }

    if(merchantAddress==""){
        $("#merchantAddress" + i).css("border","1px solid red");
        $("#AddressEmpty" + i).css("display","block");
        return false;
    }else{
        $("#merchantAddress" + i).css("border","1px solid #E5E6E7");
        $("#AddressEmpty" + i).css("display","none");
    }

    var tel=/^(0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8})|(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})(-| )?)?([0-9]{7,8})((-| |转)*([0-9]{1,4}))?$/;
    if(!tel.test(merchantTel)){
        $("#merchantTel" + i).css("border","1px solid red");
        $("#telError" + i).css("display","block");
        return false;
    }else{
        $("#merchantTel" + i).css("border","1px solid #E5E6E7");
        $("#telError" + i).css("display","none");
    }
    if(merchantEmail!="") {
        var email = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        if (!email.test(merchantEmail)) {
            $("#emailError" + i).css("display", "block");
            $("#merchantEmail" + i).css("border", "1px solid red");
            return false;
        } else {
            $("#emailError" + i).css("display", "none");
            $("#merchantEmail" + i).css("border", "1px solid #E5E6E7");
        }
    }
    if(merchantWebsite!="") {
        var url = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/;
        if (!url.test(merchantWebsite)) {
            $("#httpError" + i).css("display", "block");
            $("#merchantWebsite" + i).css("border", "1px solid red");
            return false;
        } else {
            $("#httpError" + i).css("display", "none");
            $("#merchantWebsite" + i).css("border", "1px solid #E5E6E7");
        }
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/wzUser/updateMerchantInfo", function (data) {
        merchantInfoDlg.close();
        Feng.success("修改成功!请重新打开该账户信息");
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set("userInfoId", userInfoId);
    ajax.set("merchantInfo", merchantInfo);
    ajax.start();
}

merchantInfoDlg.deleteMerchantInfo = function (event, userInfoId) {
    //提交信息
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/wzUser/deleteMerchantInfo", function (data) {
            var obj = Feng.eventParseObject(event);
            obj = obj.is('button') ? obj : obj.parent();
            obj.parent().parent().remove();
            Feng.success("删除成功!");
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("userInfoId", userInfoId);
        ajax.start();
    };
    Feng.confirm("是否刪除?", operation);
}
var addCount = 1;
var userCode = $("#userCode").val();
$(function () {
    merchantInfoDlg.addMerchantInfo = function (tag) {
        tag = addCount;
        $("#itemsArea").append(this.itemTemplate);
        $("#NmerchantTel").attr("id", "NmerchantTel" + tag);
        $("#NtelEmpty").attr("id", "NtelEmpty" + tag);
        $("#NtelError").attr("id", "NtelError" + tag);
        $("#NmerchantAddress").attr("id", "NmerchantAddress" + tag);
        $("#NAddressEmpty").attr("id", "NAddressEmpty" + tag);
        $("#NmerchantEmail").attr("id", "NmerchantEmail" + tag);
        $("#NemailError").attr("id", "NemailError" + tag);
        $("#NmerchantWebsite").attr("id", "NmerchantWebsite" + tag);
        $("#NhttpError").attr("id", "NhttpError" + tag);
        $("#NmerchantLongitude").attr("id", "NmerchantLongitude" + tag);
        $("#NmerchantLatitude").attr("id", "NmerchantLatitude" + tag);
        $("#commitButton").attr("id", "commitButton" + tag);
        $("#commitButton" + tag).click(function () {
            var merchantTel = $("#NmerchantTel" + tag).val();
            var merchantAddress = $("#NmerchantAddress" + tag).val();
            var merchantEmail = $("#NmerchantEmail" + tag).val();
            var merchantWebsite = $("#NmerchantWebsite" + tag).val();
            var merchantLongitude = $("#NmerchantLongitude" + tag).val();
            var merchantLatitude = $("#NmerchantLatitude" + tag).val();
            var merchantInfo = "{\"merchantInfo\":\"" + tag + "\",\"merchantTel\":\"" + merchantTel + "\",\"merchantAddress\":\"" + merchantAddress + "\",\"merchantEmail\":\""
                + merchantEmail + "\",\"merchantWebsite\":\"" + merchantWebsite + "\",\"merchantLongitude\":\"" + merchantLongitude + "\",\"merchantLatitude\":\"" + merchantLatitude + "\"}";

            if(merchantTel==""){
                $("#NmerchantTel" + tag).css("border","1px solid red");
                $("#NtelEmpty" + tag).css("display","block");
                return false;
            }else{
                $("#NmerchantTel" + tag).css("border","1px solid #E5E6E7");
                $("#NtelEmpty" + tag).css("display","none");
            }

            if(merchantAddress==""){
                $("#NmerchantAddress" + tag).css("border","1px solid red");
                $("#NAddressEmpty" + tag).css("display","block");
                return false;
            }else{
                $("#NmerchantAddress" + tag).css("border","1px solid #E5E6E7");
                $("#NAddressEmpty" + tag).css("display","none");
            }

            var tel=/^(0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8})|(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})(-| )?)?([0-9]{7,8})((-| |转)*([0-9]{1,4}))?$/;
            if(!tel.test(merchantTel)){
                $("#NmerchantTel" + tag).css("border","1px solid red");
                $("#NtelError" + tag).css("display","block");
                return false;
            }else{
                $("#NmerchantTel" + tag).css("border","1px solid #E5E6E7");
                $("#NtelError" + tag).css("display","none");
            }
            if(merchantEmail!="") {
                var email = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
                if (!email.test(merchantEmail)) {
                    $("#NemailError" + tag).css("display", "block");
                    $("#NmerchantEmail" + tag).css("border", "1px solid red");
                    return false;
                } else {
                    $("#NemailError" + tag).css("display", "none");
                    $("#NmerchantEmail" + tag).css("border", "1px solid #E5E6E7");
                }
            }
            if(merchantWebsite!="") {
                var url = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/;
                if (!url.test(merchantWebsite)) {
                    $("#NhttpError" + tag).css("display", "block");
                    $("#NmerchantWebsite" + tag).css("border", "1px solid red");
                    return false;
                } else {
                    $("#NhttpError" + tag).css("display", "none");
                    $("#NmerchantWebsite" + tag).css("border", "1px solid #E5E6E7");
                }
            }
            //提交信息
            var ajax = new $ax(Feng.ctxPath + "/wzUser/addMerchantInfo", function (data) {
                merchantInfoDlg.close();
                Feng.success("添加成功!请重新打开该账户信息");
            }, function (data) {
                Feng.error("添加失败!" + data.responseJSON.message + "!");
            });
            ajax.set("merchantInfo", merchantInfo);
            ajax.set("userCode", userCode);
            ajax.start();
        });
        addCount += 1;
    }
})
merchantInfoDlg.deleteItem = function (event) {
    var obj = Feng.eventParseObject(event);
    obj = obj.is('button') ? obj : obj.parent();
    obj.parent().parent().parent().remove();
}